package com.example.englishspeechproject;

import entity.Question;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {
	private static final String DATABASE_NAME="english_speech_db";	
	private static final int DATABASE_VERSION=1;
	private final Context context;	
	private DatabaseHelper databaseHelper;
	private SQLiteDatabase database;
	
	public DBAdapter(Context context){
		this.context=context;
		databaseHelper=new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper{
		
		public DatabaseHelper(Context context) {
			// TODO Auto-generated constructor stub
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	public DBAdapter open(){
		database=databaseHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		databaseHelper.close();
	}
	
	////////////////////////////////  questions table
	
	public static final String QUESTION_ROWID="_id";
	public static final String QUESTIONS_ROWQUESTION="question";
	public static final String QUESTIONS_ROWANSWER1="answer1";
	public static final String QUESTIONS_ROWANSWER2="answer2";
	public static final String QUESTIONS_ROWANSWER3="answer3";
	public static final String QUESTIONS_ROWVALIDANSWERTEXT="valid_answer_text";
	//private static final String QUESTIONS_TABLE="questions";
	
	public Question getQuestionById(long id){
		Question question=null;
		Cursor cursor=database.rawQuery("select q._id,q.question,q.answer1,q.answer2,q.answer3,q.valid_answer_text,d.term_media from questions q, dictionary d where q.valid_answer_text=d.term and q._id=?", new String[]{String.valueOf(id)});
		if(cursor!=null && cursor.moveToFirst()){
			question=new Question(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6));
		}
		return question;
	}
	
	//////////////////////////////////////////////////
	
	////////////////////////////////  dictionary table
	
	
	
	//////////////////////////////////////////////////
}
